/**
 * 基本图形
 * @date   2011-3-18
 * @author tianye
 */
package cn.edu.dlut.WingGameDemos;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import cn.edu.dlut.WingGame.BaseType.WingColor;
import cn.edu.dlut.WingGame.BaseType.WingPoint;
import cn.edu.dlut.WingGame.Graphic.GLDevice;
import cn.edu.dlut.WingGame.Graphic.GLShape;
import cn.edu.dlut.WingGame.Tools.FPSCounter;

public class Test1 extends Activity {
	private GLSurfaceView mGLView;
	private MyRender1 mRender;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		// 游戏全屏幕
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		GLDevice.setActivity(this);
		GLDevice.setBKColor(200, 200, 200);
		mGLView = new GLSurfaceView(this);
		mRender = new MyRender1();
		mGLView.setRenderer(mRender);
		setContentView(mGLView);
	}

	@Override
	protected void onPause() {
		super.onPause();
		mGLView.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mGLView.onResume();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}

class MyRender1 implements GLSurfaceView.Renderer {
	private FPSCounter fps = new FPSCounter();
	private GLShape shape = new GLShape();
	WingPoint pos[];
	WingPoint pos2[];

	public void onDrawFrame(GL10 gl) {
		GLDevice.onDrawFrame(gl);

		shape.setPointSize(1);
		for (int i = 0; i < 10; i++)
			for (int j = 0; j < 10; j++) {
				shape.drawPoint(10 + 10 * i, 10 + 10 * j, new WingColor(
						i / 10.0f, j / 10.0f, (i + j) / 20.0f, 1));
			}
		shape.setPointSize(3);
		for (int i = 0; i < 10; i++)
			for (int j = 0; j < 10; j++) {
				shape.drawPoint(70 + 10 * i, 60 + 10 * j, new WingColor(
						i / 10.0f, j / 10.0f, (i + j) / 20.0f, 1));
			}

		for (int i = 0; i < 10; i++) {
			shape.setLineWidth((i + 4) / 4.0f);
			shape.drawLine(210, 10 + 10 * i, 310, 10 + 10 * i, new WingColor(
					i / 10.0f, 0, 1, 1));
			shape.drawLine(160, 40 + 10 * i, 270, 70 + 10 * i, new WingColor(1,
					i / 10.0f, 0, 1));
		}
		shape.setLineWidth(2);
		shape.drawRect(10, 180, 110, 280, new WingColor(0f, 0f, 1f, 1f));
		shape.fillRect(50, 220, 150, 320, new WingColor(1, 1, 0, 0.5f));
		shape.drawRect(210, 180, 310, 240, new WingColor(0f, 0f, 1f, 1f));
		shape.fillRect(190, 200, 250, 300, new WingColor(1, 0, 0, 0.5f));

		shape.drawEllipse(70, 210, 210, 310, new WingColor(1f, 0f, 1f, 1f));
		shape.fillEllipse(110, 250, 250, 350, new WingColor(0, 1, 0, 0.5f));

		shape.drawCircle(240, 280, 40, new WingColor(1, 1, 0, 1f));
		shape.fillCircle(280, 320, 40, new WingColor(1, 0.5f, 0.5f, 0.5f));

		shape.drawPolygon(pos, new WingColor(0f, 1f, 0.5f, 1f));
		shape.fillPolygon(pos2, new WingColor(0f, 1f, 1f, 0.5f));

		fps.nextFrame();
		fps.drawFPS();
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		GLDevice.onSurfaceChanged(gl, width, height);

	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		GLDevice.onSurfaceCreated(gl, config);
		pos = new WingPoint[] { new WingPoint(30, 270), new WingPoint(80, 270),
				new WingPoint(90, 300), new WingPoint(70, 420),
				new WingPoint(30, 390), new WingPoint(10, 300) };
		pos2 = new WingPoint[] { new WingPoint(80, 300),
				new WingPoint(130, 320), new WingPoint(140, 380),
				new WingPoint(130, 420), new WingPoint(80, 390),
				new WingPoint(60, 360) };
	}

}
